@echo off

cls

SETLOCAL enabledelayedexpansion

set cur_path=%~dp0

set params=%*
set params_copy=%params%

set length=0
set b_length=0
set s_length=0
set t_length=0
set o_length=0

call :split_params
call :check_params
call :check_tools
call :init
call :run
call :log_back
goto :EOF

:split_params
for /f "tokens=1* delims=/ " %%a in ("%params_copy%") do (
	set params_list[%length%]=%%a
	set params_copy=%%b
	set /a length=%length%+1
)
if defined params_copy goto :split_params
exit /B 0

:print_params
echo %1's number of params is %2
set /a len=%2-1
if %2 GTR 0 (
	for /l %%i in (0,1,%len%) do (
		call echo %%%1[%%i]%%
	)
)
exit /B 0

:check_params
if "%length%"=="1" if "%params_list[0]%"=="-h" call :help
if "%length%"=="1" if "%params_list[0]%"=="help" call :help
if "%length%"=="1" if not "%params_list[0]%"=="-h" call :empty
set /a len=%length%-1
if "%params_list[0]:~0,1%"=="-" (
    set tmp_a=0
    set tmp_b=0
    for /l %%i in (0,1,%len%) do (
        if "!params_list[%%i]:~0,1!"=="-" (
            set tmp_a=!tmp_b!
            set tmp_b=%%i
            call :make_array params_list !tmp_a! !tmp_b!
        )
    )
    call :make_array params_list !tmp_b! %length%
) else (
    if "%length%"=="0" (
        set b_file[0]=accesstokenid_test
        set b_length=1
        set s_file[0]=runtest.tar.gz
        set s_length=1
        set t_file[0]=busybox-armv7l
        set t_length=1
    ) else if "%length%"=="3" (
        set b_file[0]=%params_list[0]%
        set b_length=1
        set s_file[0]=%params_list[1]%
        set s_length=1
        set t_file[0]=%params_list[2]%
        set t_length=1
    ) else (
         echo Number of params is invalid!
         goto :quit
    )
)
exit /B 0

:make_array
if "!%1[%2]!"=="-h" goto :help
set /a start=%2+1
set /a end=%3-1
if "!%1[%2]!"=="-b" (
    for /l %%i in (!start!,1,!end!) do (
        if "!%1[%%i]:~0,2!"==".\" (
            set b_file[!b_length!]=!%1[%%i]:~2!
        ) else (
            set b_file[!b_length!]=!%1[%%i]!
        )
        set /a b_length=!b_length!+1
    )
) else if "!%1[%2]!"=="-s" (
    for /l %%i in (!start!,1,!end!) do (
        if "!%1[%%i]:~0,2!"==".\" (
            set s_file[!s_length!]=!%1[%%i]:~2!
        ) else (
            set s_file[!s_length!]=!%1[%%i]!
        )
        set /a s_length=%s_length%+1
    )
) else if "!%1[%2]!"=="-t" (
    for /l %%i in (!start!,1,!end!) do (
        if "!%1[%%i]:~0,2!"==".\" (
            set t_file[!t_length!]=!%1[%%i]:~2!
        ) else (
            set t_file[!t_length!]=!%1[%%i]!
        )
        set /a t_length=%t_length%+1
    )
) else (
    for /l %%i in (!start!,1,!end!) do (
        if "!%1[%%i]:~0,2!"==".\" (
            set o_file[!o_length!]=!%1[%%i]:~2!
        ) else (
            set o_file[!o_length!]=!%1[%%i]!
        )
        set /a o_length=%o_length%+1
    )
)
set b_length=!b_length!
set s_length=!s_length!
set t_length=!t_length!
set o_length=!o_length!
exit /B 0

:check_tools
for /f %%i in ('hdc -v') do (
    set hdc_exit=%%i
)
if "%hdc_exit%"=="" (
    echo hdc not found!
	goto :quit
)

for /f %%i in ('hdc list targets') do (
    set hdc_connection=%%i
)
if "%hdc_connection%"=="[Empty]" (
    echo connection device not found!
	goto :quit
)

for /f %%i in ('python -V') do (
    set python_exit=%%i
)
if "%python_exit%"=="" (
    echo python not found!
    echo Don't run the data analysis!
)
exit /B 0

:init
for /f "delims=" %%i in ('hdc shell "mkdir /data/test"') do (
    set create_test_dir=%%i
)
if "%create_test_dir%"=="mkdir: '/data/test': File exists" (
    hdc shell "rm -rf /data/test"
	hdc shell "mkdir /data/test"
	echo /data/test exists, delete and recreate!
)
call :init_b_file
call :init_s_file
call :init_o_file
exit /B 0

:init_b_file
if %b_length% GTR 0 (
    set /a len=%b_length%-1
    for /l %%i in (0,1,!len!) do (
        set file_path=%cur_path%!b_file[%%i]!
        if not exist !file_path! (
            echo not found !file_path!
            goto :quit
        ) else (
            for /f "delims=" %%j in ('hdc file send !file_path! /data/test') do set file_send_result=%%j
            if not "!file_send_result:~0,19!" == "FileTransfer finish" (
                echo file send fail!
                goto :quit
            ) else (
                echo !b_file[%%i]! succeed send /data/test !
                hdc shell "chmod 777 /data/test/!b_file[%%i]!"
            )
        )
    )
)
exit /B 0

:init_s_file
if %t_length% GTR 0 (
    call :init_t_file
)
if %s_length% GTR 0 (
    set /a len=%s_length%-1
    for /l %%i in (0,1,!len!) do (
        set file_path=%cur_path%!s_file[%%i]!
        if not exist !file_path! (
            echo not found !file_path!
            goto :quit
        ) else (
            for /f "delims=" %%j in ('hdc file send !file_path! /data/test') do set file_send_result=%%j
            if not "!file_send_result:~0,19!" == "FileTransfer finish" (
                echo file send fail!
                goto :quit
            ) else (
                echo !s_file[%%i]! succeed send /data/test !
                hdc shell "tar -zxvf /data/test/!s_file[%%i]! -C /data/test"
            )
        )
    )
)
exit /B 0

:init_t_file
if %t_length% GTR 0 (
    set /a len=%t_length%-1
    for /l %%i in (0,1,!len!) do (
        set file_path=%cur_path%!t_file[%%i]!
        if not exist !file_path! (
            echo not found !file_path!
            goto :quit
        ) else (
            for /f "delims=" %%j in ('hdc file send !file_path! /data/test') do set file_send_result=%%j
            if not "!file_send_result:~0,19!" == "FileTransfer finish" (
                echo file send fail!
                goto :quit
            ) else (
                echo !t_file[%%i]! succeed send /data/test !
                hdc shell "mount -o rw,remount /"
                hdc shell "cp /data/test/!t_file[%%i]! /system/bin"
                hdc shell "chmod 755 /data/test/!t_file[%%i]!"
                hdc shell "chmod 755 /system/bin/!t_file[%%i]!"
                hdc shell "cd /system/bin; !t_file[%%i]! --install ."
                hdc shell "rm -rf /system/bin/tr"
                hdc shell "ln -s /system/bin/!t_file[%%i]! /system/bin/tr"
                hdc shell "mount -o ro,remount /"
            )
        )
    )
)
exit /B 0

:init_o_file
if %o_length% GTR 0 (
    set /a len=%o_length%-1
    for /l %%i in (0,1,!len!) do (
        set file_path=%cur_path%!o_file[%%i]!
        if not exist !file_path! (
            echo not found !file_path!
            goto :quit
        ) else (
            for /f "delims=" %%j in ('hdc file send !file_path! /data/test') do set file_send_result=%%j
            if not "!file_send_result:~0,19!" == "FileTransfer finish" (
                echo file send fail!
                goto :quit
            ) else (
                echo !o_file[%%i]! succeed send /data/test !
            )
        )
    )
)
exit /B 0

:run
if %b_length% GTR 0 (
    set /a len=%b_length%-1
    for /l %%i in (0,1,!len!) do (
        hdc shell "cd /data/test; ./!b_file[%%i]!"
    )
)
if %s_length% GTR 0 (
    set /a len=%s_length%-1
    for /l %%i in (0,1,!len!) do (
        hdc shell "cd /data/test/runtest; sh runtest test -t OH_RK3568_config -n OH_RK3568_skiptest -l 3600"
    )
)
exit /B 0

:log_back
hdc shell "cd /data/test/runtest; tar -zcvf log.tar.gz log"
hdc file recv /data/test/runtest/log.tar.gz %cur_path%
hdc shell "rm -rf /data/test/runtest/log.tar.gz"
exit /B 0

:help
echo Usage:
echo   test.bat [execfile] [shell.tar.gz] [busybox]
echo.
echo Examples:
echo   test.bat accesstokenid_test runtest.tar.gz busybox-armv7l
echo.
echo -b run execfile
echo -s run shell script
echo -t install tool
echo -o upload other file
echo -h print this
echo.
echo test.bat help print this
echo this is a bat func that standard for RK3568 and Hi3516
call :quit
goto :EOF

:empty
echo Please input params!
pause
exit
goto :EOF

:quit
pause
exit
goto :EOF



